<template>
  <div class="body">
    <div class="topbox">
      <span>工单编号:</span><input v-model="taskCode" type="text" placeholder="请输入">
      <span>工单状态:</span><input type="text" placeholder="请输入">
      <el-button type="primary" class="btn" @click="search">查询</el-button>
    </div>

    <div class="main">
      <el-button class="btn1" icon="el-icon-circle-plus-outline" @click="addBtn">新建</el-button>
      <el-table
        highlight-current-row
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{
          backgroundColor: 'rgb(243, 246, 251)',
          fontWeight: 500,
          padding: '6px 0'
        }"
      >
        <el-table-column
          prop="taskStatus"
          label="序号"
          type="index"
        />
        <el-table-column
          prop="taskCode"
          label="工单编号"
        />
        <el-table-column
          prop="innerCode"
          label="设备编号"
        />
        <el-table-column
          prop="taskType.typeName"
          label="工单类型"
        />
        <el-table-column
          prop="desc"
          label="工单方式"
        />
        <el-table-column
          prop="taskStatusTypeEntity.statusName"
          label="工单状态"
        />
        <el-table-column
          prop="userName"
          label="运营人员"
        />
        <el-table-column
          prop="updateTime"
          label="创建日期"
          :formatter="formatDate"
        />
        <el-table-column
          label="操作"
          width="100"
        >
          <template #default="{row}">
            <a href="#" class="cl" @click="open(row.taskId)">查看详情</a>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增dialog弹窗 -->
    <el-dialog
      title="新增工单"
      :visible.sync="dialogVisible"
      width="570px"
      @close="closeFn"
    >
      <el-form ref="userForm" label-width="120px" :model="form" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="innerCode">
              <el-input v-model="form.innerCode" size="mini" class="inputW" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工单类型" prop="typeName">
              <el-select v-model="form.typeId" placeholder="请选择" class="inputW">
                <el-option
                  v-for="item in options"
                  :key="item.typeId"
                  :label="item.typeName"
                  :value="item.typeId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="运维人员" prop="userId">
              <el-select v-model="form.userId" placeholder="请选择" class="inputW">
                <el-option
                  v-for="item in options1"
                  :key="item.userId"
                  :label="item.userName"
                  :value="item.userId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="desc">
              <el-input
                v-model="form.desc"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4}"
                placeholder="请输入内容"
                class="inputW"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <div class="footer">
          <el-button class="cancel" @click="closeFn">取 消</el-button>
          <el-button type="primary" class="confirm" @click="submit">确 定</el-button>
        </div>
      </span>
    </el-dialog>

    <FindInfo :id="id" ref="FindInfo" :visible.sync="visible" />

    <el-footer>
      <span>共{{ total }}条记录</span>&nbsp;&nbsp;
      <span>第{{ queryObj.pageIndex }}/{{ totalPage }}页</span>
      <el-pagination
        background
        style="float: right"
        :current-page="+queryObj.pageIndex"
        :page-size="+queryObj.pageSize"
        prev-text="上一页"
        next-text="下一页"
        layout="prev, next"
        :total="+total"
        @current-change="handleCurrentChange"
      />
    </el-footer>
  </div>
</template>

<script>
import { workOrderList, workOrderType, getOperatorList } from '@/api/workOrder'
import FindInfo from '@/views/work-order-management/yunyin/components/findInfo.vue'
export default {
  name: 'DikedeIndex',
  components: {
    FindInfo
  },
  data () {
    return {
      id: 0,
      visible: false,
      totalPage: 0,
      tableData: [],
      queryObj: {
        pageIndex: 1,
        pageSize: 10,
        isRepair: false
      },
      form: {
        innerCode: '',
        typeId: '',
        userId: '',
        desc: ''
      },
      total: null,
      dialogVisible: false,
      rules: {
        innerCode: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        typeName: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      },
      options: [],
      options1: [],
      value: '',
      currentForm: null,
      taskCode: null
    }
  },
  watch: {
    form: {
      deep: true,
      async handler (value) {
        if (value.innerCode !== '') {
          const data = await workOrderType(value.innerCode)
          const res = await getOperatorList(value.innerCode)
          console.log(res)
          this.options = data
          this.options1 = res
        }
      }
    }
  },
  created () {
    this.dataList()
  },
  methods: {
    async dataList () {
      const data = await workOrderList(this.queryObj)
      this.queryObj.pageIndex = data.pageIndex
      this.queryObj.pageSize = data.pageSize
      this.total = data.totalCount
      this.totalPage = data.totalPage
      this.tableData = data.currentPageRecords
    },

    async type () {
      const data = await workOrderType(this.form.innerCode)
      console.log(data)
      this.options = data
      console.log(this.options)
    },

    addBtn () {
      this.dialogVisible = true
      this.type()
    },

    submit () {
      this.dialogVisible = false
      this.$message.warning('演示系统，不支持此操作')
    },
    closeFn () {
      this.dialogVisible = false
      this.options = []
      this.options1 = []
      this.form.innerCode = ''
      this.form.productType = ''
      this.$refs.userForm.resetFields()
    },
    async open (id) {
      console.log(id)
      this.id = +id
      this.visible = true
    },
    handleCurrentChange (val) {
      this.queryObj.pageIndex = val
      this.dataList()
    },
    formatDate (row, column) {
      // 获取单元格数据
      const datac = row[column.property]
      return datac.replace(/\-/g, '.').replace('T', ' ')
    },
    search () {
      if (this.taskCode) {
        this.tableData = this.tableData.filter(item => {
          // return console.log(item.taskCode)
          return item.taskCode.includes(this.taskCode)
        })
      } else {
        this.dataList()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  border-radius: 10px !important;
}
.btn1 {
  margin: 20px 0;
}
.footer {
  text-align: center;
}
.cancel {
  background-color: rgb(251, 244, 240) !important;
    color: rgb(101, 91, 86) !important;
    border: none;
}

.confirm {
  background: linear-gradient(135deg, rgb(255, 151, 67), rgb(255, 94, 32)) !important;
  border: none;
}
.el-footer {
  line-height: 60px;
  background-color: #fff;
  span {
    font-size: 16px;
    color: #dbdfe5;
  }
  .el-pagination {
    margin-top: 10px;
  }
}
::v-deep .btn-prev:disabled {
  background: #edf0f9 !important;
}
::v-deep .btn-next:disabled {
  background: #edf0f9 !important;
}
::v-deep .btn-prev{
 width: 70px;
 height: 32px;
 background-color: #d5ddf8 !important;
}
::v-deep .btn-next{
  margin: 0 20px;
 width: 70px;
 height: 32px;
 background-color: #d5ddf8 !important;
}
::v-deep .el-table {
 td {
  padding: 8px 0;
  border: none;
 }
 &::before {
  height: 0;
 }
}

.inputW{
  width: 360px;
}
.inoputq{
  width: 360px;
  height: 84px;
}
.body {
  padding: 20px;
  .topbox {
    margin-bottom: 20px;
    background-color: #fff;
    padding-left: 17px;
    height: 64px;
    line-height: 64px;
    span {
      color: #606266;
      font-size: 14px;
      margin-right: 12px;
    }
    input {
      width: 209px;
      border: 1px solid #d8dde3;
      height: 36px;
      line-height: 36px;
      padding: 0 30px 0 15px;
      border-radius: 4px;
      margin-right: 8px;
    }
    input::placeholder{
      color: #ccc;
      font-size: 14px;
    }
    .btn{
      text-align: center;
      height: 36px;
      width: 80px;
      margin-right: 10px;
      background-color: #5f84ff;
    }
  }
  .main{
    background-color: #fff;
    .btn1{
      width: 80px;
      height: 36px;
      border:none;
      margin-right: 10px;
      border-radius: 4px;
      font-size: 14px;
      color: #fff;
      background: linear-gradient(135deg,#ff9743,#ff5e20)!important
    }
  }
}
.cl {
  color: #5f84ff;
  font-size: 14px;
}
</style>
